/*
 * @author java_deng
 * @date 2024/12/4 10:16
 * @description
 */
package org.kaka.controller;

import com.github.pagehelper.Page;
import org.kaka.dto.FileDto;
import org.kaka.po.FilePo;
import org.kaka.service.impl.FileServiceImpl;
import org.kaka.utils.PageResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

@RestController
@RequestMapping("/file")
public class FileController extends BaseController{
    @Autowired
    private FileServiceImpl fileService;

    @PostMapping("/uploadSwiper")
    public Object upload(@RequestParam("file") MultipartFile file){
        Integer save = save(file,"swiper");
        if(save==0){
            return fail(new String("上传的文件类型错误,只能上传png,jpeg,jpg类型的文件"));
        }
        return success(save);
    }

    @PostMapping("/uploadImage")
    public Object uploadImage(@RequestParam("file") MultipartFile file){
        Integer save = save(file,"image");
        if(save==0){
            return fail(new String("上传的文件类型错误,只能上传png,jpeg,jpg类型的文件"));
        }
        return success(save);
    }

    @GetMapping("/swiperList")
    public Object list(){
        return success(fileService.fileDtoList(1));
    }


    @GetMapping("/swiperListByPage")
    public Object swiperListByPage(FilePo filePo){
        filePo.setType(1);
        Page<FileDto> fileDtos = fileService.fileDtoListByPage(filePo);
        PageResponseData<FileDto> fileDtoPageResponseData = new PageResponseData<>();
        fileDtoPageResponseData.setResult(fileDtos.getResult());
        fileDtoPageResponseData.setPageCount(fileDtos.getPages());
        fileDtoPageResponseData.setItemCount(fileDtos.getTotal());
        return success(fileDtoPageResponseData);
    }

    @GetMapping("/imageList")
    public Object imageList(){
        return success(fileService.fileDtoList(2));
    }

    @GetMapping("/updateDelete")
    public Object updateDelete(Integer id){
        if(id==null){
            return fail("id不能为空");
        }
        fileService.updateDelete(id);
        return success("修改成功");
    }

    @GetMapping("/delete")
    public Object delete(Long id){
        if(id==null){
            return fail("id不能为空");
        }
        fileService.delete(id);
        return success("删除成功");
    }

    private Integer save(MultipartFile file,String type){
        String originName = file.getOriginalFilename();
        Set<String> set = new HashSet<>();
        set.add(".png");
        set.add(".jpeg");
        set.add(".jpg");
        int count = 0;
        for(int i = 0; i < originName.length(); i++){
            if(originName.charAt(i) == '.'){
                count = i;
                break;
            }
        }
        String endName = originName.substring(count);
        String randomName=Double.toString(Math.random()).substring(2,6);
        String fileType = originName.substring(count + 1);
        if(!set.contains(endName)){
            return 0;
        }
        try {
            String name=System.currentTimeMillis()+randomName+endName;
            String path="D:\\nginx-1.22.1\\html\\"+type+"\\"+name;
            file.transferTo(new File(path));
            FileDto fileDto=new FileDto();
            fileDto.setPath("http://localhost:80/"+type+"/"+name);
            fileDto.setTitle(originName);
            fileDto.setType(type.equals("image")?2:1);
            fileDto.setIsDeleted(1);
            fileService.save(fileDto);
            return fileDto.getId();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
